package InterviewOfCollections;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

/*
 * Author: JacksonCoder
 * FileName: BasicOfQueue
 * Version: 1.0.0
 */
public class BasicOfQueue {
    public static void main(String[] args) throws InterruptedException {
//        QueueExample();
//        PriorityQueueExample();
        BlockingQueueExample();

    }

    // 队列
    public static void BlockingQueueExample() throws InterruptedException {
        // 创建一个容量为2的阻塞队列
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(2);

        // 生产者线程
        Thread producer = new Thread(() -> {
            try {
                blockingQueue.put("Task1");
                System.out.println("Added: Task1");
                blockingQueue.put("Task2");
                System.out.println("Added: Task2");
                // 队列满时阻塞
                blockingQueue.put("Task3");
                System.out.println("Added: Task3");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        // 消费者线程
        Thread consumer = new Thread(() -> {
            try {
                Thread.sleep(2000); // 模拟处理延迟
                System.out.println("Removed: " + blockingQueue.take());
                System.out.println("Removed: " + blockingQueue.take());
                System.out.println("Removed: " + blockingQueue.take());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        producer.start();
        consumer.start();
        producer.join();
        consumer.join();
    }

    public static void PriorityQueueExample(){
        // 创建一个优先级队列
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

        // 添加元素
        priorityQueue.add(30);
        priorityQueue.add(10);
        priorityQueue.add(20);

        // 打印队列
        System.out.println("Priority Queue: " + priorityQueue);

        // 移除并打印元素
        while (!priorityQueue.isEmpty()) {
            // 移除并打印队列中的最小元素
            System.out.println("Polling: " + priorityQueue.poll());
        }

        System.out.println("Priority Queue: " + priorityQueue);
        priorityQueue.add(30);
        priorityQueue.add(10);
        priorityQueue.add(20);

        //移除指定元素
        priorityQueue.remove(20);
        System.out.println("Priority Queue: " + priorityQueue);
    }

    public static void QueueExample(){
        // 创建一个队列
        Queue<String> queue = new LinkedList<>();

        // 添加元素到队列
        queue.add("Alice");
        queue.add("Bob");
        queue.add("Charlie");

        // 打印队列
        System.out.println("Initial Queue: " + queue);

        // 查看队列头部元素
        System.out.println("Head of the queue: " + queue.peek());

        // 移除队列头部元素
        String removedElement = queue.poll();
        System.out.println("Removed Element: " + removedElement);

        // 打印队列剩余元素
        System.out.println("Queue after removal: " + queue);

        // 检查队列是否为空
        System.out.println("Is the queue empty? " + queue.isEmpty());

        // 获取队列头部元素
        System.out.println(queue.element());

        // 添加元素到队列
        queue.offer("Jackson");
        System.out.println("Queue after removal: " + queue);

        // 检查队列是否包含某个元素
        System.out.println(queue.contains("Jackson"));

        // 检查队列是否为空
        System.out.println(queue.isEmpty());
    }

}
